var lottery = require('./code/lottery/lottery');
var fs = require("fs");
var path = require("path");
var query = require("./db/db");

var date = new Date();
function def1() {
    function def2() {
        var year = date.getFullYear();

        var month = date.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        var day = date.getDate();
        if (day < 10) {
            day = "0" + day;
        }
        var x = year + '-' + month + '-' + day;
        date.setDate(date.getDate() - 1);

        return x;
    }

    return def2;
}
var ctrl = def1();

function realtime(b) {
    var stopday = 0;
    var thattime = '2016-06-01 18:30';//计算标准
    var thatid = 77874;
    var plustime;
    if (b - thatid > 264) {
        xx = Math.floor((b - thatid) / 265) + stopday;//过了多少天
        yy = (b - thatid) % 265;             //当天的第几期
        plustime = yy * 5 + (60 * 24) * xx;
    } else {
        plustime = (b - 77874) * 5;
    }
    var time = new Date(thattime.replace("-", "/"));
    time.setMinutes(time.getMinutes() + plustime, time.getSeconds(), 0);
    var year = time.getFullYear().toString();
    var month = (time.getMonth() + 1).toString();
    if (month < 10) {
        month = '0' + month
    }
    var day = time.getDate().toString();
    if (day < 10) {
        day = '0' + day
    }
    var hour = time.getHours().toString();
    if (hour < 10) {
        hour = '0' + hour
    }
    var mins = time.getMinutes().toString();
    if (mins < 10) {
        mins = '0' + mins
    }
    time = year + '-' + month + '-' + day + ' ' + hour + ':' + mins;
    return (time)
}

function pk10day(todaystr) {
    console.log('进入code,接受参数为： ' + todaystr);
    lottery.getdate.bjpk10day(todaystr, 1, function (json_data) {
        console.log('数据加载完成');
        console.log(json_data);
        if (json_data == 'empty') {
            init();
            throw new Error('当日数据为空')
        }
        if (json_data != 'error' && json_data.length) {
            var total = json_data.length;
            var times = 0;

            function write() {
                console.log('total为' + total + 'times为' + times);
                //查询id是否存在
                var data = json_data[times];
                times += 1;
                console.log('此次抓取期数为' + data.gamenum);
                //对ballnum处理为数组
                var ballnum = data.ballnum;
                var balls = ballnum.split(',');
                //对gametime处理
                var daystr = data.gametime.substr(0, 10);
                var sql1 = 'select * from result where gameid = ' + data.gamenum + ' and gamekind = "pk10";';
                //写入数据
                var sql2 = 'insert app.result(gamekind,gameid,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,daystr,opentime) values("pk10"' +
                    ',"' + data.gamenum + '","' + balls[0] + '","' + balls[1] + '","' + balls[2] + '","' + balls[3] +
                    '","' + balls[4] + '","' + balls[5] + '","' + balls[6] + '","' + balls[7] + '","' + balls[8] +
                    '","' + balls[9] + '","' + daystr + '","' + realtime(data.gamenum) + '");';
                query(sql1, function (err, vals, fields) {
                    if (err) {
                        console.log(err);
                    }
                    if (!vals.length) {
                        console.log('进入sql写入阶段');
                        query(sql2, function (err, vals, fields) {
                            if (err) {
                                console.log(err);
                            }
                            console.log(data.gamenum + '期,写入完成');
                        })
                    } else {
                        console.log('此期已存在数据库,跳过');
                    }
                })
            }

            function run() {
                if (total - times > 0) {
                    write();
                } else {
                    console.log('完成此次程序');
                    clearInterval(x);
                    init();
                }
            }

            var x = setInterval(run, 1000);
        } else {
            console.log('拿到数据不正确,准备重试');
            setTimeout(pk10day(todaystr), 5000);
        }
    });
}


function init() {
    var time = new Date();
    console.log('——————————————————分割线——————————————————');
    console.log('抓取时间:' + time);
    pk10day(ctrl());
}

init();
/*setInterval(init, 2000000);*/


process.on('uncaughtException', function (err) {
    console.log(err.stack);
});
